﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SimpleExpression
{
    public class GroupByExpression : Expression
    {
        private AttributeExpressionCollection attributes;
        private EntityMetadata entityMetadata;

        public GroupByExpression()
        {
            this.attributes = new AttributeExpressionCollection();
        }

        public GroupByExpression(EntityMetadata entityMetadata)
            :this()
        {
            this.entityMetadata = entityMetadata;   
        }

        public void Add(string property)
        { 
            this.Add(GetAttributeExpression(property));
        }

        public void Add(PropertyMetadata propertyMetadata)
        {
            this.Add(new AttributeExpression(propertyMetadata));
        }

        public void Add(AttributeExpression attribute)
        {
            this.attributes.Add(attribute);
        }

        public AttributeExpressionCollection Attributes
        {
            get { return this.attributes; }
        }

        private AttributeExpression GetAttributeExpression(string name)
        {
            // Si se encuentra el physicalName entonces se agrega un AttributeExpression de lo contrario un ViewAttributeExpression
            switch (this.entityMetadata.Properties.Contains(name))
            {
                case MappingType.None:
                    break;
                case MappingType.Physical:
                    return new AttributeExpression(this.entityMetadata[name]);
                case MappingType.Platform:
                    return new ViewAttributeExpression(this.entityMetadata.Properties[name, MappingType.Platform]);
                default:
                    break;
            }

            throw new NotImplementedException();
        }

        #region Expression

        public override void Accept(ExpressionVisitor visitor)
        {
            visitor.Visit(this);
        }

        #endregion
    }
}
